package com.lianyi.commonsdk.util.security;

import android.util.Log;

import com.lianyi.commonsdk.util.LogUtil;
import com.lianyi.commonsdk.util.SPUtils;
import com.lianyi.commonsdk.util.SPUtilsName;

/**
 * Created by leilei on 2018/7/23.
 */

public class AesUtils {

    /**
     * 数据AES加密
     *
     * @param push 需要加密的内容
     * @return
     */
    public static String AesEncryption(String push) {
        AESOperator myAESO = new AESOperator();
        if (push.contains("data=")) {
            push = push.substring(push.indexOf("data=") + 5);
        }
        LogUtil.e("OkgoManager未加密Data==", "OkgoManager未加密Data||22" + push);
        String AEStr = SPUtils.getInstance(SPUtilsName.HOSPITAL_TABLE).getString(SPUtilsName.HOSPITAL_AES);
        try {
            myAESO.sKey = AEStr.substring(0, 16);
            myAESO.ivParameter = AEStr.substring(16, 32);
            push = myAESO.encrypt(push);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//        push= URLEncoder.encode(push);
//        push = "hid=" + hid + "&data=" + push;
        LogUtil.e("OkgoManager加密Data==", "OkgoManager加密Data||" + push);
        return push;
    }

    /**
     * 数据AES加密
     *
     * @param push 需要加密的内容
     * @param
     * @return
     */
    public static String AesEncryption2(String push) {
//        int hid = PreferenceUtils.getInstance(Myapplication.getContext()).getHospitalhid();
        AESOperator myAESO = new AESOperator();
        push = push.substring(push.indexOf("data=") + 5);
        Log.e("未加密Data==", "未加密Data||" + push);
        String AEStr = SPUtils.getInstance(SPUtilsName.HOSPITAL_TABLE).getString(SPUtilsName.HOSPITAL_AES);
        try {
            myAESO.sKey = AEStr.substring(0, 16);
            myAESO.ivParameter = AEStr.substring(16, 32);
            push = myAESO.encrypt(push);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//        push = "hid=" + hid + "&type=" + type + "&data=" + push;
        Log.e("加密Data==", "加密Data||" + push);
        return push;
    }

    /**
     * 返回的AES解密
     *
     * @return
     */
    public static String AesDecode(String mdate) {
        AESOperator myAESO = new AESOperator();
        String aestr = SPUtils.getInstance(SPUtilsName.HOSPITAL_TABLE).getString(SPUtilsName.HOSPITAL_AES);
        myAESO.sKey = aestr.substring(0, 16);
        myAESO.ivParameter = aestr.substring(16, 32);
        String m = "";
        try {
            m = myAESO.decrypt(mdate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return m;
    }

}
